import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//查
public class JdbcDemo4 {
    public static void main(String[] args) throws SQLException {
        // 1. 创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java117?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("1205");

        // 2. 和数据库服务器建立连接
        Connection connection = dataSource.getConnection();

        // 3. 构造 SQL
        String sql = "select * from student";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        // 4. 执行 SQL, 返回结果是一个 对象, 称为 ResultSet
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            // 针对一行进行处理了
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(id + " " + name);
        }

        // 5. 关闭连接等资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
}
